package com.lrkj.back.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lrkj.back.entity.TbDepartment;
import com.lrkj.back.entity.TbPackageMuban;
import com.lrkj.back.entity.ZsKeshiKucun;
import com.lrkj.back.entity.ZsShenqing;
import com.lrkj.back.service.TbPackageMubanService;
import com.lrkj.back.service.ZsKeshiKucunService;
import com.lrkj.back.service.ZsShenqingShoushubaoMingxiService;
import com.lrkj.back.entity.TbEmployee;
import com.lrkj.back.entity.ZsShenqingShoushubaoMingxi;
import com.lrkj.back.service.TbDepartmentService;
import com.lrkj.back.service.TbSeqService;
import com.lrkj.back.service.ZsShenqingService;
import com.lrkj.back.Const;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.cbons.mamisay.parent.exception.BusinessException;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("keshikucun")
public class ZsKeshiKucunController extends BaseController{

	@Autowired
	private ZsKeshiKucunService kucunService;
	@Autowired
	private TbDepartmentService departmentService;
	@Autowired
	private TbPackageMubanService mubanservice;
	@Autowired
	private ZsShenqingService shenqingService;
	@Autowired
	private TbSeqService seqService;
	@Autowired
	private ZsShenqingShoushubaoMingxiService ssbservice;

	//临床科室--财产管理  （点击查询）
	@RequestMapping("keshikucunList.do")
	public ModelAndView keshikucunList(String searchTxt, HttpServletRequest request, HttpServletResponse response) throws BusinessException{
		Integer pageNo = request.getParameter("pageIndex")==null? 1 :Integer.valueOf(request.getParameter("pageIndex"));
		Map<String,Object> map =new HashMap<>();
//		TbDepartment department = null;
		/*if (StringUtils.isNotBlank(searchTxt)) {
			map.put("keshiName", searchTxt);
			department = departmentService.findOne(map);
		}
		if (department != null) {
			map.put("keshiId", department.getDepartmentId());
		}*/
		map.put("keshiId", searchTxt);
		map.put("pageNo", pageNo);
		map.put("pageSize", Const.PAGE_SIZE);
		try {
			PageInfo<ZsKeshiKucun> page = kucunService.findAllPageInfo(map);
			List<TbDepartment> departments = departmentService.findAllDepartment();
			request.setAttribute("keshilist", departments);
			request.setAttribute("keshikucun", page.getList());
			request.setAttribute("searchTxt", searchTxt==null?"":searchTxt);
			request.setAttribute("total", page.getPages());
		} catch (Exception e){
			e.printStackTrace();
		}
		return new ModelAndView("linchuang/caichanguanli/caichan");
	}
	
	@RequestMapping("/caichanApplyEdit.do")
	public ModelAndView yicipinApplyEdit(String id, HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		ZsShenqing shenqing = (id == null) ? new ZsShenqing() : shenqingService.selectByPrimaryKey(id);
//		TbEmployee emp =(TbEmployee)request.getSession().getAttribute("emp");
//		Integer departId = null;
//		if (emp != null) {aq
//			departId = emp.getDepartmentId();
//		}
//		Map<String,Object> map =new HashMap<String,Object>();
//		if(StringUtils.isNotBlank(id)){
//			map.put("shenqingKeshi", id);
//		}
//		List<ZsShenqingYicipinMingxi> mingxiList = shenqingservice.findAllShenqingMingxi(map);
//		if(mingxiList == null)
//			mingxiList = new ArrayList<ZsShenqingYicipinMingxi>();
//		shenqing.setMingxiList(mingxiList);
        //
		Map<String, Object> map = new HashMap<>();
		map.put("packageCategory", 1);
//		map.put("packageDepartid", departId);
		//xianzai
		List<TbPackageMuban> proList = mubanservice.findAllPackageNoPageList(map);
		request.setAttribute("proList", proList);
		request.setAttribute("proJson", JSON.toJSON(proList));
		request.setAttribute("shenqing", shenqing);
		return new ModelAndView("/linchuang/caichanguanli/caichan_shenqing");
	}
	
	@RequestMapping("/caichanApplySave.do")
	@ResponseBody
	public Map yicipinApplySave(HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<>();
		//保存时先保存主表，再保存手术包那个表
		//32 277
		try {

			String[] proId = request.getParameterValues("productId");
			String[] proName = request.getParameterValues("productName");
			String[] proNum = request.getParameterValues("productNum");
			SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMdd");
			String shenqingliushui = sdf.format(new Date())+seqService.seqVal("shenqingno");
			ZsShenqing sq = new ZsShenqing();
			TbEmployee emp =(TbEmployee)request.getSession().getAttribute("emp");
			sq.setShenqingKeshi(emp.getDepartmentId().toString());
//			sq.setShenqingShenherenTiaoma(emp.getEmployeeNo());
			sq.setShenqingShenqingrenTiaoma(emp.getEmployeeNo());
			sq.setShenqingLiushui(shenqingliushui);
			sq.setRecordTime(new Date());
			sq.setShenqingFenlei(0);
			sq.setShenqingTime(new Date());
			//默认审核状态0
			sq.setShenqingShenheZhuangtai(0);
			sq.setShifouXuyao(1);
			shenqingService.insert(sq);
			ZsShenqingShoushubaoMingxi mingxi = null;
			//同时加入多 个对象
			for(int i = 0 ;i<proId.length;i++){
				mingxi = new ZsShenqingShoushubaoMingxi();
				mingxi.setShenqingLiushui((shenqingliushui));
				mingxi.setShenqingMingxiLiushui(sdf.format(new Date())+seqService.seqVal(" "));
				mingxi.setShenqingWupinbaoMuban(proId[i]);
				mingxi.setShenqingWupinbaoMingcheng(proName[i]);
				mingxi.setShenqingWupinbaoShuliang(Integer.valueOf(proNum[i]));
				mingxi.setFafangZhuangtai(0);
				mingxi.setShenqingKeshi(emp.getDepartmentId().toString());
				mingxi.setRecordTime(new Date());
				ssbservice.insert(mingxi); 
			}
			rtnMap.put("status", "true");
			rtnMap.put("msg", "编辑成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "编辑失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
	
	@RequestMapping("keshikucunEdit.do")
	public ModelAndView keshikucunEdit(Integer id, HttpServletRequest request, HttpServletResponse response)throws BusinessException{
		ZsKeshiKucun keshiKucun = (id == null) ? new ZsKeshiKucun() : kucunService.selectByPrimaryKey(id);
		request.setAttribute("kucun", keshiKucun);
		return new ModelAndView("linchuang/caichanguanli/caichan_edit");
	}
	
	//保存库存财产(直接修改数据)
	/*@RequestMapping("kucnEdit.do")
	public void kucnEdit(Integer kucunId,ZsKeshiKucun keshiKucun, HttpServletRequest request, HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<Object, Object>();
		try {
			Integer beforediushi = 0;
			Integer beforexinzeng = 0;
			String yidiushi = request.getParameter("yidiushi");
			String yixinzeng = request.getParameter("yixinzeng");
			if (StringUtils.isNotBlank(yidiushi)) {
				beforediushi = Integer.valueOf(yidiushi);
			}
			if (StringUtils.isNotBlank(yixinzeng)) {
				beforexinzeng = Integer.valueOf(yixinzeng);
			}
			if (keshiKucun != null) {
				if (keshiKucun.getYiyouKucun() == null) {
					keshiKucun.setYiyouKucun(0);
				}
				if (keshiKucun.getXinzengKucun() == null) {
					keshiKucun.setXinzengKucun(0);
				}
				if (keshiKucun.getDiushiKucun() == null) {
					keshiKucun.setDiushiKucun(0);
				}
				keshiKucun.setYiyouKucun(keshiKucun.getYiyouKucun()+keshiKucun.getXinzengKucun()-keshiKucun.getDiushiKucun());
				keshiKucun.setRecordTime(new Date());
				keshiKucun.setDiushiKucun(beforediushi+keshiKucun.getDiushiKucun());
				keshiKucun.setXinzengKucun(beforexinzeng+keshiKucun.getXinzengKucun());
				kucunService.updateByPrimaryKey(keshiKucun);
			}
			rtnMap.put("status", "true");
			rtnMap.put("msg", "保存成功");
		} catch (Exception e) {
			rtnMap.put("statu s", "false");
			rtnMap.put("msg", "保存失败");
			e.printStackTrace();
		}
		writeByMapString(rtnMap);
	}*/
	 
	//保存库存财产(新增一条数据)
	@RequestMapping("kucunEdit.do")
	@ResponseBody
	public Map kucnEdit(Integer kucunId,ZsKeshiKucun keshiKucun, HttpServletRequest request, HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<Object, Object>();
		try {
			Integer beforediushi = 0;
			Integer beforexinzeng = 0;
			String yidiushi = request.getParameter("yidiushi");
			String yixinzeng = request.getParameter("yixinzeng");
			if (StringUtils.isNotBlank(yidiushi)) {
				beforediushi = Integer.valueOf(yidiushi);
			}
			if (StringUtils.isNotBlank(yixinzeng)) {
				beforexinzeng = Integer.valueOf(yixinzeng);
			}
			if (keshiKucun != null) {
				ZsKeshiKucun kucun = new ZsKeshiKucun();
				if (keshiKucun.getYiyouKucun() == null) {
					keshiKucun.setYiyouKucun(0);
				}
				if (keshiKucun.getXinzengKucun() == null) {
					keshiKucun.setXinzengKucun(0);
				}
				if (keshiKucun.getDiushiKucun() == null) {
					keshiKucun.setDiushiKucun(0);
				}
				kucun.setKeshiId(keshiKucun.getKeshiId());
				kucun.setMubanId(keshiKucun.getMubanId());
				kucun.setYiyouKucun(keshiKucun.getYiyouKucun()+keshiKucun.getXinzengKucun()-keshiKucun.getDiushiKucun());
				kucun.setRecordTime(new Date());
				kucun.setDiushiKucun(keshiKucun.getDiushiKucun());
				kucun.setXinzengKucun(keshiKucun.getXinzengKucun());
				kucun.setKucunSort(keshiKucun.getKucunSort());
				if (kucun.getYiyouKucun() >= 0) {
					kucunService.insert(kucun);
					rtnMap.put("status", "true");
					rtnMap.put("msg", "保存成功");
				}else {
					rtnMap.put("status", "false");
					rtnMap.put("msg", "库存余量不能小于0");
				}
			}
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "保存失败");
			e.printStackTrace();
		}
		return rtnMap;
	}

	@RequestMapping("yicipin.do")
	public ModelAndView yicipin(HttpServletRequest request, HttpServletResponse response) throws BusinessException{
		Map<String, Object> map = new HashMap<String, Object>();
		TbEmployee employee = (TbEmployee)request.getSession().getAttribute("emp");
		if (employee != null) {
			map.put("keshiId", employee.getDepartmentId());
		}
		map.put("kucunSort", 2);
		List<ZsKeshiKucun> keshiKucuns = kucunService.findAllKucun(map);
		request.setAttribute("kucuns", keshiKucuns);
		request.setAttribute("kucunsJson", JSON.toJSON(keshiKucuns));
		return new ModelAndView("/linchuang/caichanguanli/ycpshiyong");
	}
	
	@RequestMapping("oneOffshiyong.do")
	@ResponseBody
	public Map oneOffshiyong(ZsKeshiKucun keshiKucun, HttpServletRequest request, HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<Object, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		List<ZsKeshiKucun> list = new ArrayList<ZsKeshiKucun>();
		TbEmployee employee = (TbEmployee)request.getSession().getAttribute("emp");
		String[] proId = request.getParameterValues("mubanId");
//		String[] proName = request.getParameterValues("productName");
		String[] proNum = request.getParameterValues("xinzengKucun");
		if (employee != null) {
			map.put("keshiId", employee.getDepartmentId());
		}
		for (int i = 0; i < proId.length; i++) {
			map.put("mubanId", proId[i]);
			keshiKucun = kucunService.findOne(map);
			if (Integer.valueOf(proNum[i]) <= keshiKucun.getYiyouKucun()) {
				keshiKucun.setYiyouKucun(keshiKucun.getYiyouKucun() - Integer.valueOf(proNum[i] ));
				list.add(keshiKucun);
			}
		}
		if (!list.isEmpty() && list.size() == proId.length) {
			kucunService.updateByBatch(list);
			rtnMap.put("status", "true");
			rtnMap.put("msg", "保存成功");
		} else {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "库存数不足");
		}
		return rtnMap;
	}
}
